package cn.edu.zafu.bigdata.cccnnn.workfive;

import java.io.*;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

class Student implements Serializable {
    String id;
    String name;
    String phone;

    public Student(String id, String name, String phone) {
        this.id = id;
        this.name = name;
        this.phone = phone;
    }

    @Override
    public String toString() {
        return "Student: id=" + id + ", name=" + name + ", phone=" + phone;
    }

    // Getters and setters
    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }
}

class MStudent {
    public static Map<String, Student> students = new HashMap<>();

    public static void addStudent(Scanner scanner) {
        System.out.print("id: ");
        String id = scanner.nextLine();
        if (students.containsKey(id)) {
            System.out.println("id重复，请重新输入。");
            return;
        }
        System.out.print("name: ");
        String name = scanner.nextLine();
        System.out.print("phone: ");
        String phone = scanner.nextLine();
        Student student = new Student(id, name, phone);
        students.put(id, student);
        System.out.println("OK!");
        saveStudents();
    }

    public static void findStudent(Scanner scanner) {
        System.out.print("id: ");
        String id = scanner.nextLine();
        while (!id.equalsIgnoreCase("QUIT")) {
            if (students.containsKey(id)) {
                System.out.println(students.get(id));
            } else {
                System.out.println("未找到该学生。");
            }
            System.out.print("id（输入QUIT退出）: ");
            id = scanner.nextLine();
        }
    }

    public static void updateStudent(Scanner scanner) {
        System.out.print("id: ");
        String id = scanner.nextLine();
        while (!id.equalsIgnoreCase("QUIT")) {
            if (students.containsKey(id)) {
                System.out.print("new name: ");
                String name = scanner.nextLine();
                System.out.print("new phone: ");
                String phone = scanner.nextLine();
                Student student = students.get(id);
                student.setName(name);
                student.setPhone(phone);
                System.out.println("already update!");
                saveStudents();
            } else {
                System.out.println("未找到该学生。");
            }
            System.out.print("id（输入QUIT退出）: ");
            id = scanner.nextLine();
        }
    }

    public static void deleteStudent(Scanner scanner) {
        System.out.print("id: ");
        String id = scanner.nextLine();
        while (!id.equalsIgnoreCase("QUIT")) {
            if (students.containsKey(id)) {
                students.remove(id);
                System.out.println("学生信息已删除。");
                saveStudents();
            } else {
                System.out.println("未找到该学生。");
            }
            System.out.print("id（输入QUIT退出）: ");
            id = scanner.nextLine();
        }
    }

    public static void saveStudents() {
        String filePath = TestStudent.getFileName();
        try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(filePath))) {
            oos.writeObject(students);
        } catch (IOException e) {
            System.out.println("无法保存学生信息。");
            e.printStackTrace();
        }
    }

    public static Map<String, Student> loadStudents() {
        String filePath = TestStudent.getFileName();
        try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(filePath))) {
            return (Map<String, Student>) ois.readObject();
        } catch (IOException | ClassNotFoundException e) {
            System.out.println("无法加载学生信息。");
            e.printStackTrace();
            return new HashMap<>();
        }
    }
}

public class TestStudent {
    private static final String FILE_NAME = "C:\\Users\\zjqzs\\IdeaProjects\\LearnJava\\src\\main\\java\\cn\\edu\\zafu\\bigdata\\cccnnn\\workfive\\students.dat";

    public static String getFileName() {
        return FILE_NAME;
    }

    public static Map<String, Student> students;

    public static void main(String[] args) {
        students = MStudent.loadStudents();
        Scanner scanner = new Scanner(System.in);
        while (true) {
            System.out.println("======= 学生信息管理系统 =======");
            System.out.println("1. 添加学生信息");
            System.out.println("2. 检索学生信息");
            System.out.println("3. 更新学生信息");
            System.out.println("4. 删除学生信息");
            System.out.println("5. 退出程序");
            System.out.print("请输入操作编号 (1-5): ");
            int p = scanner.nextInt();
            scanner.nextLine();

            switch (p) {
                case 1:
                    MStudent.addStudent(scanner);
                    break;
                case 2:
                    MStudent.findStudent(scanner);
                    break;
                case 3:
                    MStudent.updateStudent(scanner);
                    break;
                case 4:
                    MStudent.deleteStudent(scanner);
                    break;
                case 5:
                    MStudent.saveStudents();
                    System.out.println("程序已退出。");
                    System.exit(0);
                    break;
            }
        }
    }
}